package com.hit.server.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.server.api.entity.*;
import com.hit.server.api.dto.RobotQueryDTO;
import com.hit.server.api.dto.RobotSaveDTO;
import com.hit.server.api.dto.RobotUpdateDTO;
import com.hit.server.api.vo.RobotVO;
import com.hit.server.biz.mapper.*;
import com.hit.server.api.service.RobotService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

/**
 * 机器人
 *
 * @author kehanjiang
 * @date 2025-04-03 17:24:22
 */
@Service
@RequiredArgsConstructor
public class RobotServiceImpl extends ServiceImpl<RobotMapper, RobotEntity> implements RobotService {

    private final RobotClientMapper robotClientMapper;
    private final RobotCarMapper robotCarMapper;
    private final RobotGroupMapper robotGroupMapper;
    private final SysProjectMapper sysProjectMapper;

    private RobotVO convertToVO(Object o) {
        if (o instanceof RobotEntity) {
            RobotVO vo = new RobotVO();
            BeanUtil.copyProperties(o, vo);
            //设置其他参数
            Optional.ofNullable(vo.getClientId()).ifPresent(clientId -> {
                RobotClientEntity client = robotClientMapper.selectById(clientId);
                if (client != null) {
                    vo.setClientName(client.getName());
                }
            });
            Optional.ofNullable(vo.getCarId()).ifPresent(carId -> {
                RobotCarEntity car = robotCarMapper.selectById(carId);
                if (car != null) {
                    vo.setCarName(car.getName());
                }
            });
            Optional.ofNullable(vo.getGroupId()).ifPresent(groupId -> {
                RobotGroupEntity group = robotGroupMapper.selectById(groupId);
                if (group != null) {
                    vo.setGroupName(group.getName());
                }
            });
            Optional.ofNullable(vo.getProjectId()).ifPresent(projectId -> {
                SysProjectEntity project = sysProjectMapper.selectById(projectId);
                if (project != null) {
                    vo.setProjectName(project.getName());
                }
            });
            return vo;
        }
        return null;
    }

    private void preCheck(RobotEntity entity, Boolean update) {
        Long id = entity.getId();
        //String name = entity.getName();
        if (update) {//更新操作
            Assert.notNull(id, "id不可为空！");
        } else {
            //Assert.hasText(name, "name不可为空！");
        }
    }

    @Override
    public Page<RobotVO> getRobotPage(Page page, RobotQueryDTO dto) {
        RobotEntity entity = new RobotEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setName(null);
        entity.setSn(null);
        final String name = dto.getName();
        final String sn = dto.getSn();
        LambdaQueryWrapper<RobotEntity> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.like(StringUtils.hasText(name), RobotEntity::getName, name);
        wrapper.like(StringUtils.hasText(sn), RobotEntity::getSn, sn);
        wrapper.orderByAsc(RobotEntity::getId);
        return (Page<RobotVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }

    @Override
    public RobotVO getRobotById(Long id) {
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }


    @Override
    public Boolean saveRobot(RobotSaveDTO dto) {
        RobotEntity entity = new RobotEntity();
        BeanUtils.copyProperties(dto, entity);
        Long groupId = entity.getGroupId();
        if (groupId != null) {
            RobotGroupEntity group = robotGroupMapper.selectById(groupId);
            Long projectId = group.getProjectId();
            entity.setProjectId(projectId);
        }
        this.preCheck(entity, false);
        return this.save(entity);
    }

    @Override
    public Boolean updateRobotById(RobotUpdateDTO dto) {
        RobotEntity entity = new RobotEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, true);
        Long groupId = entity.getGroupId();
        if (groupId != null) {
            RobotGroupEntity group = robotGroupMapper.selectById(groupId);
            Long projectId = group.getProjectId();
            entity.setProjectId(projectId);
        }else {
            entity.setProjectId(null);
        }
        return this.updateById(entity);
    }

    @Override
    public Boolean removeRobotBatchByIds(List<Long> ids) {
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }
}